package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.domain.po.BfyDmsRunningLog;
import com.ruoyi.system.domain.po.BfyDmsRunningLogExample;
import com.ruoyi.system.domain.po.DmsRunningLog;
import com.ruoyi.system.domain.po.DmsRunningLogExample;
import com.ruoyi.system.mapper.BfyDmsRunningLogMapper;
import com.ruoyi.system.mapper.DmsRunningLogMapper;
import com.ruoyi.system.service.DmsRunningLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DmsRunningLogServiceImpl implements DmsRunningLogService {

    @Resource
    private DmsRunningLogMapper dmsRunningLogMapper;

    @Resource
    private BfyDmsRunningLogMapper bfyDmsRunningLogMapper;

    @Override
    @DataSource(DataSourceType.MASTER)
    public DmsRunningLog getDmsRunningLogs() {
        DmsRunningLogExample dmsRunningLogExample = new DmsRunningLogExample();
        dmsRunningLogExample.createCriteria().andStatusEqualTo((byte) 1);
        dmsRunningLogExample.setOrderByClause("created_at desc");
        PageHelper.startPage(1, 1);
        List<DmsRunningLog> dmsRunningLogs = dmsRunningLogMapper.selectByExample(dmsRunningLogExample);
        if (CollectionUtil.isNotEmpty(dmsRunningLogs)) {
            return dmsRunningLogs.get(0);
        }
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void insertRunningLog(DmsRunningLog dmsRunningLog) {
        dmsRunningLogMapper.insertSelective(dmsRunningLog);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void updateRunningLog(DmsRunningLog dcsRunningLog) {
        dmsRunningLogMapper.updateByPrimaryKeySelective(dcsRunningLog);
    }

    @Override
    @DataSource(DataSourceType.MASTER)
    public BfyDmsRunningLog getBfyDmsRunningLogs() {
        BfyDmsRunningLogExample dmsRunningLogExample = new BfyDmsRunningLogExample();
        dmsRunningLogExample.createCriteria().andStatusEqualTo((byte) 1);
        dmsRunningLogExample.setOrderByClause("created_at desc");
        PageHelper.startPage(1, 1);
        List<BfyDmsRunningLog> dmsRunningLogs = bfyDmsRunningLogMapper.selectByExample(dmsRunningLogExample);
        if (CollectionUtil.isNotEmpty(dmsRunningLogs)) {
            return dmsRunningLogs.get(0);
        }
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void insertBfyRunningLog(BfyDmsRunningLog dmsRunningLog) {
        bfyDmsRunningLogMapper.insertSelective(dmsRunningLog);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void updateBfyRunningLog(BfyDmsRunningLog dcsRunningLog) {
        bfyDmsRunningLogMapper.updateByPrimaryKeySelective(dcsRunningLog);
    }

    @Override
    public Date getLastUpdateTime() {
        return dmsRunningLogMapper.getBlsZlInfoLastUpdateTime();
    }
}
